﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SACM.Base;
using SACM.Common.Objects;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
using CrystalDecisions.Shared;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace SACM.PageCommon
{
    public partial class ReportViewer : BasePage
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            IdItem = 4;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            string nombrePdf;
            if (!Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(ReportName))
                {
                    ReportDocument rptDoc = new ReportDocument();
                    //CRReportViewer.ParameterFieldInfo = ReportViewerParams;
                    foreach (CrystalDecisions.Shared.ParameterField param in ReportViewerParams)
                    {
                        rptDoc.SetParameterValue(param.Name, param.CurrentValues);
                    }

                    rptDoc.Load(Server.MapPath("../Reports/" + ReportName));
                    rptDoc.SetDataSource(ReportDataSorce);
                    ////rptDoc.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.DefaultPaperSize;
                    nombrePdf = Guid.NewGuid() + ".pdf";
                    string ruta = Server.MapPath("../RptPdfTemp/") + nombrePdf;
                    rptDoc.ExportToDisk(ExportFormatType.PortableDocFormat, ruta);

                    string rutaWeb = "../RptPdfTemp/" + nombrePdf;
                    object print = ViewParametros.Find(delegate(Parametro p) { return p.Nombre == "esImprimir"; }).Valor;
                    SetPdf(print.ToString() == "1" ? true : false, rutaWeb);
                    ////ScriptManager.RegisterStartupScript(Page, typeof(Page), "print", "printPdf('" + ResolveClientUrl(rutaWeb) + "');", true);
                }
            }
        }

        protected void BtnImprimir_Click(object sender, EventArgs e)
        {

        }

        private void SetPdf(bool print, string path)
        {
            if (print)
            {
                var notUriPath = Server.MapPath(path);

                var doc = new Document();
                var reader = new PdfReader(notUriPath);
                using (var memoryStream = new MemoryStream())
                {
                    var writer = PdfWriter.GetInstance(doc, memoryStream);
                    doc.Open();

                    // this action leads directly to printer dialogue
                    var jAction = PdfAction.JavaScript("this.print(true);\r", writer);
                    writer.AddJavaScript(jAction);

                    var cb = writer.DirectContent;
                    doc.AddDocListener(writer);

                    for (var p = 1; p <= reader.NumberOfPages; p++)
                    {
                        doc.SetPageSize(reader.GetPageSize(p));
                        doc.NewPage();
                        var page = writer.GetImportedPage(reader, p);
                        var rot = reader.GetPageRotation(p);
                        if (rot == 90 || rot == 270)
                            cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(p).Height);
                        else
                            cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0);
                    }

                    reader.Close();
                    doc.Close();
                    File.WriteAllBytes(notUriPath, memoryStream.ToArray());
                }
            }

            iFramePDF.Attributes.Add("src", path);
        }

        protected void BtnExcel_Click(object sender, EventArgs e)
        {
            string nombrePdf;
            ReportDocument rptDoc = new ReportDocument();
            foreach (CrystalDecisions.Shared.ParameterField param in ReportViewerParams)
            {
                rptDoc.SetParameterValue(param.Name, param.CurrentValues);
            }

            rptDoc.Load(Server.MapPath("../Reports/" + ReportName));
            rptDoc.SetDataSource(ReportDataSorce);
            nombrePdf = Guid.NewGuid() + ".pdf";
            string ruta = Server.MapPath("../RptPdfTemp/") + nombrePdf;
            rptDoc.ExportToHttpResponse(ExportFormatType.Excel, Response, true, "exportReport");
        }
    }
}